package com.ttl.bc.ap.stkrefund.action;




import java.util.HashMap;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


import com.ttl.bc.ap.stkrefund.db.BCStkRefundBO;
import com.ttl.common.util.CommonUtils;

public class BCStkRefundAction extends DispatchAction {
	
	public ActionForward invfind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRefundAction #mode :blankFindInv");		
				
		return mapping.findForward("invfind");
	}
	
	
	public ActionForward findInvSelected(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRefundAction #mode :findInvSelected");
		
		//request.setAttribute("docNo", CommonUtils.getMaxBCPurchaseOrderDocNo());
		
		BCStkRefundBO bo = new BCStkRefundBO();		
		BCStkRefundForm sf = (BCStkRefundForm)bo.findInv(form, request);
		request.setAttribute("sf", sf);
//		ArrayList results = (ArrayList)bo.findInv(form, request);
//		request.setAttribute("resultList_inv", results);				
//		request.setAttribute("countId", results.size());
//		ArrayList results_item = (ArrayList)bo.findItem(results); 
//		request.setAttribute("resultList_item", results_item);
		
		return mapping.findForward("invData");
	}	
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRefundAction #mode :add ");
		
		BCStkRefundBO bo = new BCStkRefundBO();						
		if (bo.add(form, request, response)==true) {
			request.setAttribute("message", "บันทึกข้อมูลใหม่เรียบร้อย");
			return mapping.findForward("success_add");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_add");
		}
	}

	public ActionForward cancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		BCStkRefundBO bo = new BCStkRefundBO();			
		BCStkRefundForm sf = (BCStkRefundForm)bo.findStkRefund(request);
		request.setAttribute("sf", sf);		
		
		return mapping.findForward("success_cancel");		
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCAPInvoiceAction #mode :delete");
						
		if (new BCStkRefundBO().delete(form, request, response)==true) {
			request.setAttribute("message", "ยกเลิกเอกสารเรียบร้อย");
			return mapping.findForward("success_delete");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_delete");
		}
	}
	
	public ActionForward showFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCAPInvoiceAction #mode :showFind");
		
		new BCStkRefundBO().showFind(request);
		return mapping.findForward("success_showFind2");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward print(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String docNo = (String)request.getParameter("docNo");
		
		String docNoParam = " i.docNo='" + docNo +"'";
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCAPInvoiceAction #mode :print ");		
		System.out.println("#docNo = "+docNo);
				
		String nameOfReport = "/ireport/p22.jasper";			
							
		HashMap hashMap = new HashMap();
		hashMap.put("docNoParam", docNoParam);

		if(CommonUtils.runReportToPdf(response, getServlet(), hashMap, nameOfReport)==true) {
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}				
//		Connection connection;
//		ServletOutputStream servletOutputStream = response.getOutputStream();
//		
//		InputStream reportStream = getServlet().getServletConfig()
//							.getServletContext().getResourceAsStream(nameOfReport);
//		
//		response.setContentType("application/pdf");		
//
//		Class.forName("net.sourceforge.jtds.jdbc.Driver");		
//
//		connection = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.1.2:1433/ttl51;user=sa;password=admin@123");
//
//		JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, hashMap, connection);
//
//		connection.close();
//		servletOutputStream.flush();
//		servletOutputStream.close();

		return null;
	}	
}
